package com.xszx.controller;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.xszx.entity.*;
import com.xszx.entity.dto.RecordDTO;
import com.xszx.entity.enums.ConStatusEnum;
import com.xszx.service.TAssetService;
import com.xszx.service.TContractService;
import com.xszx.service.TDeptService;
import com.xszx.service.TRecordService;
import com.xszx.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class ContractController {

    @Autowired
    private TContractService contractService;

    @Autowired
    TAssetService assetService;

    @Autowired
    TRecordService recordService;

    @Autowired
    TDeptService deptService;




    /**
     * 获取所有合同的数据信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectContract")
    public List<TContract> selectContract(HttpServletRequest request){

        TUser user = (TUser) request.getSession().getAttribute("user");
        TDept dept = deptService.getDeptByDid(user.getDid());

        List<TContract> tContracts = contractService.selectContract(dept.getDname());
        log.info("查询所有合同：{}",tContracts);
        return tContracts;
    }


    @ResponseBody
    @RequestMapping("/insertContract")
    public int insertContract(TContract contract,HttpServletRequest request){

        long nextId = new SnowFlakeUtil().getNextId();
        contract.setCid(nextId);
        log.info("添加合同：{}",contract);
        contract.setCreatename(contract.getArmorDept());
        contract.setCreatedate(new Date());
        TAsset asset = assetService.selectAssetByAcode(contract.getAcode());
        contract.setAid(asset.getAid());
        contract.setCstatus(ConStatusEnum.theApprovalIsPending.getName());
        System.out.println("接收到的参数对象是："+contract);
        if(contract!=null){
            int i = contractService.insertContract(contract);
            insertRecordDto(contract,request);
            return i;
        }
        throw new NullPointerException("没有值传递");
    }

    /**
     * 将合同信息存入审批记录中
     * @param contract
     * @return
     */
    public void insertRecordDto(TContract contract,HttpServletRequest request){

        TUser user = (TUser) request.getSession().getAttribute("user");
        //根据资产名称查对应的信息
        TAsset asset = assetService.selectAssetByAid(contract.getAid(),user.getDid());
        System.out.println(asset);
        //根据部门名称查对应的信息
        TDept dept = deptService.getDeptByName(contract.getArmorDept());

        TRecord record = new TRecord();

        record.setRid(new SnowFlakeUtil().getNextId());
        record.setAid(asset.getAid());
        record.setUid(user.getId());
        record.setDid(dept.getDid());
        record.setBorrowdate(contract.getCreatedate());
        //申请结束日期
        record.setRstatus(contract.getCstatus());
        record.setRtype(contract.getCtype());
        recordService.insertRecordDtro(record);

    }


    /**
     * 根据合同
     * @param cid
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectContractByCidAndDept")
    public TContract selectContractByCidAndDept(@RequestParam("cid") long cid,HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        TDept dept = deptService.getDeptByDid(user.getDid());
        TContract tContract = contractService.selectContractByCid(cid, dept.getDname());

        return tContract;
    }



}
